package com.siqi.hr.system.common.controller;

import com.siqi.hr.system.common.model.filter.QueryDataEntity;
import com.siqi.hr.system.common.service.CommonOperationService;
import com.siqi.hr.system.common.service.RecyclebinService;
import com.siqi.hr.system.common.sql.Condition;
import com.siqi.hr.system.common.sql.SField;
import com.siqi.hr.system.common.sql.STable;
import com.siqi.hr.system.common.utils.QueryDataUtil;
import com.siqi.hr.system.custom.service.FieldEmployeeService;
import com.siqi.hr.system.organization.entity.Employee;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * 回收站通用控制器
 * @version    2016-09-18
 * @author     xiaogq
 * @since      JDK1.8.0_12
 */
@Controller
@RequestMapping("Recyclebin")
public class RecyclebinController {

    @Resource
    RecyclebinService recyclebinService;

    @Autowired
    private FieldEmployeeService fieldEmployeeService;

    @Autowired
    private CommonOperationService commonOperationService;

    /**
     * 初始化回收站列表页面
     * @author   xiaogq
     * @return
     * @throws Exception
     */
    @RequestMapping("initRecyclebin")
    public ModelAndView initRecyclebin(HttpServletRequest request, HttpSession session) throws Exception {
        //获得当前登陆系统用户信息
        Employee emp = (Employee)request.getSession().getAttribute("emp");
        //获取实体id
        Integer entity_id = Integer.parseInt(request.getParameter("entity_id"));
        //获取实体列头、字段信息
        Map<String,Object> columnMap = fieldEmployeeService.getEntityColumns(emp,entity_id, false,true);

        ModelAndView mav = new ModelAndView();
        mav.addObject("tableCode", entity_id);
        mav.addObject("columns", columnMap.get("columns"));
        mav.addObject("fields", columnMap.get("fields"));
        mav.setViewName("common/trash/recyclebin_list");
        return mav;
    }

    /**
     * 查询有回收站实体列表数据方法
     * @author   xiaogq
     * @param request
     * @param session
     * @return
     * @throws Exception
     */
    @RequestMapping("queryRecyclebin")
    @ResponseBody
    public Map<String, Object> queryRecyclebin(HttpServletRequest request, HttpSession session) throws Exception {
        //获取搜索条件、当前登录用户、客户化实体以及分页数据信息
        QueryDataEntity queryData = QueryDataUtil.getQueryDataEntity(request);
        //设置列表数据分页查询
        queryData.setUsepager(true);
        queryData.setFlag(true);
        queryData.setValidFlag(false);
        //获取列头操作按钮
        Map<String, Object> resultMap =  commonOperationService.queryCustomObjectAll(queryData);
        return resultMap;
    }

    /**
     * 恢复选中、恢复全部、删除选中、删除全部方法
     * @author  xiaogq
     * @param request
     * @param session
     */
    @RequestMapping("recyclebinHandel")
    @ResponseBody
    public JSONObject recyclebinHandel(HttpServletRequest request, HttpSession session){
        JSONObject obj = new JSONObject();
        //获取实体id
        Integer entity_id = Integer.parseInt(request.getParameter("entity_id"));
        //获取选择记录id
        String records = request.getParameter("records");
        //获取操作状态
        Integer code = Integer.parseInt(request.getParameter("code"));
        boolean flag = recyclebinService.recyclebinHandel(entity_id, records, code);
        obj.put("flag",flag);
        return obj;
    }
}
